<?php

namespace app\common\model;

use think\Model;



class Base extends Model

{

    public function __construct($data = [])

    {

        parent::__construct($data);

    }

    /**

     * 获取单条数据并返回数组

     * @param string $where

     * @param string $feild

     * @return array|null

     */

    public function getOne($where = "", $feild = "")

    {

        $res = $this->where($where)->field($feild)->find();

        if ($res) {

            return $res->toArray();

        } else {

            return null;

        }

    }



    /**

     * 获取多条数据

     * @param string $where

     * @param string $feild

     * @return false|\PDOStatement|string|\think\Collection

     */

    public function getAll($where = "", $feild = "", $order = ['id' => 'desc'])

    {

        $res = $this->where($where)->field($feild)->order($order)->select();

        return $res;

    }



    /**

     * 获取数据条数

     * @param string $where

     * @return int count

     */

    public function getCount($where =""){

        $res = $this->where($where)->count();

        return $res;

    }



    /**

     * 分页获取数据

     * @param string $where

     * @param string $feild

     * @param int $offset

     * @param int $limit

     * @param array $order

     * @return false|\PDOStatement|string|\think\Collection

     */

    public function getLimit($where = "", $feild = "", $offset = 0, $limit = 20, $order = ['id' => 'desc'])

    {

        $res = $this->where($where)->field($feild)->limit($offset,$limit)->order($order)->select();

        return $res;

    }



    /**

     * 获取分页

     * @param string $where

     * @param string $feild

     * @param int $offset

     * @param int $limit

     * @param array $order

     * @return mixed

     */

    public function getList($where = "", $feild = "", $offset = 0, $limit = 20, $order = ['id' => 'desc'])

    {

        $list = $this->where($where)->field($feild)->order($order)->limit($offset,$limit)->select();        

        $count = $this->where($where)->count();

        $data['total'] = $count;

        $data['rows'] = $list;

        return $data;

    }





    /**

     * 添加或更新，并反回所有数据

     * @param string $where

     * @param string $data

     * @return array|false|int

     */

    public function addOrUp($data = '', $where = '')

    {

        $res = $this->allowField(true)->save($data, $where);

        if ($res) {

            return $this->toArray();

        } else {

            return $res;

        }

    }



    /**

     * 删除

     * @param $where

     * @return int

     * @throws \think\Exception

     */

    public function del($where)

    {

        return $this->where($where)->delete();

    }



    /**

     * 自增

     * @param $where

     * @param $feild

     * @param $number

     * @return int|true

     * @throws \think\Exception

     */

    public function inc($where, $feild, $number)

    {

        return $this->where($where)->setInc($feild, $number);

    }

    /**

     * 自减

     * @param $where

     * @param $feild

     * @param $number

     * @return int|true

     * @throws \think\Exception

     */

    public function dec($where, $feild, $number)

    {

        return $this->where($where)->setDec($feild, $number);

    }
}

